package io.github.talelin.latticy.controller.v1;

import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.dto.rule.chargePlan.ChargePlanDTO;
import io.github.talelin.latticy.service.ChargePlanService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

/**
 * 计费方案前端控制器
 *
* @author generator@wulongbo
* @since 2022-08-23
*/
@RestController
@RequestMapping("/v1/charge-plan")
@Validated
@PermissionModule(value = "计费方案")
public class ChargePlanController {

    @Autowired
    private ChargePlanService chargePlanService;

    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "添加计费方案")
    public CreatedVO create(@RequestBody @Validated ChargePlanDTO dto) {
        chargePlanService.addChargePlan(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "编辑计费方案")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated ChargePlanDTO dto) {
        chargePlanService.updateChargePlan(id,dto);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除计费方案")
    public DeletedVO delete(@PathVariable @Positive(message = "{charge-plan.id.not-null}") Integer id) {
        chargePlanService.deleteChargePlan(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "查询计费方案详情")
    public FindResultVO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return FindResultVO.success(chargePlanService.getChargePlanById(id));
    }

    @GetMapping("/page")
    @GroupRequired
    @PermissionMeta(value = "查询计方案分页")
    public PageResponseVO<ChargePlanPageVO> page(
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            String name
    ) {
        return chargePlanService.getChargePlanPage(page, count, name);
    }

}
